package perl.lexical.classes;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Identifier extends TokenClass {
	
	private final Pattern charPattern = Pattern.compile("^[\\$,\\@,\\w]$");
	
	@Override
	public Token generateToken(String lexem) {

		Pattern p = Pattern.compile("[\\$\\@]?[_,a-z, A-Z].*");
		Matcher m = p.matcher(lexem);
		String result = "";
		if (m.matches()) {
			
			for(int i = 0; i<lexem.length(); i++){
				if(charPattern.matcher(lexem.substring(i,i+1)).matches()){
					result += lexem.substring(i,i+1);
				}else{
					break;
				}
			}
			
			String symb = result.substring(0,1);
			
			if(symb.equals("$")){
				return new Token(TokenClassEnum.SCALAR_IDENT, result);
			}
			if(symb.equals("@")){
				return new Token(TokenClassEnum.ARRAY_IDENT, result);
			}
			
			return new Token(className, result);
		}

		return null;
	}

	public Identifier() {
		className = TokenClassEnum.IDENTIFIER;
	}
}
